﻿function AddComment(ideaId)
{
  if (ideaId != null)
  {
    jQuery.ajax("/Idea/AddComment/" + ideaId,
    {
      data:{Content:document.getElementById('fieldComment').value},
      success: function (data, textStatus, jqXHR)
      {
        if (data.status == "success")
        {
          GetComments(ideaId);
          document.getElementById('fieldComment').value = "";
        }
        else
        {
          alert(data.message);
        }
      }
    });
  }
  else
  {
    alert("You must specify an idea to comment on");
  }
}

function GetComments(ideaId)
{
  if (ideaId != null)
  {
    jQuery.ajax("/Idea/GetComments/" + ideaId,
    {
      success: function (data, textStatus, jqXHR)
      {
        var template = document.getElementById('commentTemplate').firstElementChild;
        var cont = document.getElementById('postContainer');

        while (cont.firstChild != null) //remove existing items
        {
          cont.removeChild(cont.firstChild);
        }

        for (var i = 0; i < data.data.length; i++) //insert new ones
        {
          var item = template.cloneNode(true);
          var content = item.querySelector(".comment-text");
          content.innerHTML = data.data[i].Content;
          var author = item.querySelector(".comment-author");
          author.innerHTML = data.data[i].Author;
          cont.appendChild(item);
        }
      }
    });
  }
  else
  {
    alert("You must specify an idea to comment on");
  }
}